package com.zmc.lostfound.post.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.BooleanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zmc.lostfound.common.exception.MyException;
import com.zmc.lostfound.common.utils.Result;
import com.zmc.lostfound.post.vo.PostStarSaveVO;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmc.lostfound.post.dao.PostStarDao;
import com.zmc.lostfound.post.entity.PostStarEntity;
import com.zmc.lostfound.post.service.PostStarService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ZMC, gxc
 * description 帖子&用户收藏业务实现类
 * @email 2869581855@qq.com
 * @date 2023-07-05 15:49:44
 */
@Service
public class PostStarServiceImpl extends ServiceImpl<PostStarDao, PostStarEntity> implements PostStarService {

    /**
     * 按主键id查询帖子&收藏信息
     *
     * @param id 主键id
     * @return PostStarEntity
     */
    @Override
    public PostStarEntity queryInfoById(Long id) {
        if (id == null || id <= 0) {
            MyException.throwException("查询失败: 帖子&收藏id不能为null且必须大于0");
        }
        return this.getById(id);
    }

    /**
     * 按帖子id查询帖子&收藏信息
     *
     * @param postId 帖子id
     * @return List<Long> 用户ids
     */
    @Override
    public List<Long> queryInfoByPostId(Long postId) {
        if (postId == null || postId <= 0) {
            MyException.throwException("查询失败: 帖子id不能为null且必须大于0");
        }
        List<PostStarEntity> postStarList = this.query().select("user_id").eq("post_id", postId).list();
        if (CollectionUtil.isEmpty(postStarList)) {
            return null;
        }
        return postStarList.stream().map(PostStarEntity::getUserId).collect(Collectors.toList());
    }

    /**
     * 按用户id查询帖子&收藏信息
     *
     * @param userId 用户id
     * @return List<Long> 帖子ids
     */
    @Override
    public List<Long> queryInfoByUserId(Long userId) {
        if (userId == null || userId <= 0) {
            MyException.throwException("查询失败: 用户id不能为null且必须大于0");
        }
        List<PostStarEntity> postStarList = this.query().select("post_id").eq("user_id", userId).list();
        if (CollectionUtil.isEmpty(postStarList)) {
            return null;
        }
        return postStarList.stream().map(PostStarEntity::getPostId).collect(Collectors.toList());
    }

    /**
     * 判断用户是否收藏该帖子
     *
     * @param userId 用户id
     * @param postId 帖子id
     * @return Boolean
     */
    @Override
    public Boolean isLike(Long userId, Long postId) {
        if (userId == null || userId <= 0) {
            MyException.throwException("查询失败: 用户id不能为null且必须大于0");
        }
        if (postId == null || postId <= 0) {
            MyException.throwException("查询失败: 帖子id不能为null且必须大于0");
        }
        Integer count = this.query().eq("post_id", postId).eq("user_id", userId).count();
        return BooleanUtil.isTrue(count > 0);
    }

    /**
     * 新增帖子&用户收藏信息
     *
     * @param postStarSaveVO 帖子&收藏信息新增/修改请求体
     * @return Result
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result insertPostUser(PostStarSaveVO postStarSaveVO) {
        Long userId = postStarSaveVO.getUserId();
        PostStarEntity postStarEntity = this.query()
                .eq("user_id", userId)
                .eq("post_id", postStarSaveVO.getPostId()).one();
        if (postStarEntity != null) {
            MyException.throwException("新增失败: 帖子&收藏关联信息已存在，请勿重复添加");
        }
        postStarEntity = BeanUtil.copyProperties(postStarSaveVO, PostStarEntity.class);
        if (!this.save(postStarEntity)) {
            MyException.throwException("新增帖子&收藏关联信息失败");
        }
        return Result.ok("新增成功");
    }

    /**
     * 修改帖子&用户收藏信息
     *
     * @param postStarSaveVO 帖子&收藏信息新增/修改请求体
     * @return Result
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result updatePostUser(PostStarSaveVO postStarSaveVO) {
        Long userId = postStarSaveVO.getUserId();
        PostStarEntity postStarEntity = this.query()
                .eq("user_id", userId)
                .eq("post_id", postStarSaveVO.getPostId()).one();
        if (postStarEntity != null) {
            MyException.throwException("修改失败: 帖子&收藏关联信息已存在");
        }
        postStarEntity = BeanUtil.copyProperties(postStarSaveVO, PostStarEntity.class);
        if (!this.updateById(postStarEntity)) {
            MyException.throwException("修改帖子&收藏关联信息失败");
        }
        return Result.ok("修改成功");
    }

    /**
     * 按主键ids批量删除帖子&用户收藏信息
     *
     * @param ids 主键ids
     * @return Result
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result deleteBatchByIds(List<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            MyException.throwException("删除失败: 主键ids为空");
        }
        this.removeByIds(ids);
        return Result.ok("删除成功");
    }

    /**
     * 按帖子id或用户id删除帖子&用户收藏信息
     *
     * @param postId 帖子id
     * @param userId 用户id
     * @return Result
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result deleteByPostIdAndUserId(Long postId, Long userId) {
        if (postId == null && userId == null) {
            MyException.throwException("帖子id与收藏人id不能同时为null");
        }
        this.remove(new QueryWrapper<PostStarEntity>()
                .eq(postId != null, "post_id", postId)
                .eq(userId != null, "user_id", userId));
        return Result.ok("删除成功");
    }

}